<template>
  <ql-dialog
    :title="props.adminId ? '编辑管理员' : '创建管理员'"
    :confirm="confirm"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <el-form
      ref="form"
      v-loading="formLoading"
      label-width="100px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item
        label="姓名："
        prop="name"
      >
        <el-input
          v-model="formData.name"
          placeholder="请输入角色姓名"
        />
      </el-form-item>
      <el-form-item
        label="账号："
        prop="username"
      >
        <el-input
          v-model="formData.username"
          placeholder="请输入账号"
        />
      </el-form-item>
      <el-form-item
        v-if="!props.adminId"
        label="密码："
        prop="password"
      >
        <el-input
          v-model="formData.password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item
        label="手机号："
        prop="mobile"
      >
        <el-input
          v-model="formData.mobile"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item
        label="邮箱："
        prop="email"
      >
        <el-input
          v-model="formData.email"
          placeholder="请输入邮箱"
        />
      </el-form-item>
      <el-form-item
        label="角色："
        prop="roleIdList"
      >
        <el-select
          v-model="formData.roleIdList"
          style="width: 100%;"
          multiple
          filterable
          allow-create
          default-first-option
          placeholder="请选择角色标签"
        >
          <el-option
            v-for="item in options"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="状态："
        prop="status"
      >
        <el-radio
          v-model="formData.status"
          :value="1"
        >
          启用
        </el-radio>
        <el-radio
          v-model="formData.status"
          :value="0"
        >
          禁用
        </el-radio>
      </el-form-item>
    </el-form>
  </ql-dialog>
</template>

<script setup>
import { allRoles, userInfo, userSave, userUpdate } from '@/api/setting/admin'
import { test } from 'qin-lantern/util'

const props = defineProps({
  adminId: {
    type: Number,
    default: null
  }
})
const emit = defineEmits(['success', 'update:admin-id'])

const formData = ref({
  username: '',
  name: '',
  password: '',
  email: '',
  mobile: '',
  roleIdList: [],
  status: 1
})
const formRules = reactive({
  name: [{ required: true, message: '请输入角色姓名', trigger: 'blur' }],
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  mobile: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (!test.mobile(value)) {
          callback(new Error('请输入正确手机号'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  email: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (!test.email(value)) {
          callback(new Error('请输入正确邮箱'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  roleIdList: [
    { required: true, message: '请选择角色', trigger: 'blur' }
  ]
})

// 获取角色
const formLoading = ref(false)
const handleDialogOpen = async () => {
  formLoading.value = true
  Promise.all([getToles(), getInfo()]).finally(() => {
    formLoading.value = false
  })
}

const options = ref([])
const getToles = async () => {
  const data = await allRoles()
  options.value = data
}

const getInfo = async () => {
  if (!props.adminId) return
  const data = await userInfo(props.adminId)
  formData.value = data
}

const form = ref(null)
// 确认
const confirm = async () => {
  await form.value?.validate()
  props.adminId ? await userUpdate(formData.value) : await userSave(formData.value)

  emit('success')
  ElMessage.success('保存成功')
}
// 关闭
const handleDialogClosed = () => {
  emit('update:admin-id', null)
  form.value?.resetFields() // 清除表单数据
}

</script>

<style scoped lang='scss'>
</style>
